//
//  CYVisitorView.swift
//  CYWeiBo
//
//  Created by Mac on 16/7/12.
//  Copyright © 2016年 Mac. All rights reserved.
//

import UIKit
import SnapKit


class CYVisitorView: UIView {

    //  定义登录或者注册执行的闭包
    var loginClosure: (()->())?
    
    // MARK:  懒加载控件
    // 旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 罩层
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // icon
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        //  点击事件
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        // 设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        // 设置文本和文本颜色
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        
        return button
    }()
    
    //  登录按钮按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置文本及文本颜色
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    //  消息内容
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        
        // 多行显示
        label.numberOfLines = 0
        
        //居中显示
        label.textAlignment = .Center
        
        return label
    }()
    
    
    // 使用手写代码的方式调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    // 使用xib或者storyboard 会调用这个，没有实现xib 会给你提示
    required init?(coder aDecoder: NSCoder) {
        // 支持xib写法
        super.init(coder: aDecoder)
        
        backgroundColor = UIColor.yellowColor()
        
        setupUI()
    }
    
    // 添加视图设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        // 添加控件
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(244)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        
    }
    
    // 开启旋转动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 动画目的地
        animation.toValue = 2 * M_PI
        // 动画时长
        animation.duration = 20
        // 动画的重复次数
        animation.repeatCount = MAXFLOAT
        // 失去焦点不释放资源
        animation.removedOnCompletion = false
        
        // 执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    
    }
    
    
    //  修改访客视图信息
    func updateVisitorInfo(message: String?, imageName: String?) {
        //  不是首页(消息,发现,我的)
        if let msg = message, imgName = imageName {
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            //  隐藏旋转视图
            cycleImageView.hidden = true
        } else {
            //  是首页
            //  显示旋转视图
            cycleImageView.hidden = false
            //  执行旋转动画
            startAnimation()
        }
        
        
    }
    
    
    // MARK: --点击事件
    @objc private func registerAction() {
        
        print("注册点击")
        loginClosure?()
    }
    @objc private func loginAction() {
        
        print("登录点击")
        loginClosure?()
    }
    
    
    
    
    
    private func test() {
        
        //  使用手动布局
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
    }
    
    
}
